/**
 * 命令文件
 */

const { sequelize } = require('./db/common');
const [a, b, argv] = process.argv;
const { menuType } = require('./utils/Consts');
const md5 = require('md5-node');

(async () => {
    if (argv === 'alter') {
        await sequelize.sync({ alter: true });
    }
    else if (argv === 'force') {
        await sequelize.sync({ force: true });
    }
    else {
        await sequelize.sync();
    }
    const { menu_list, roles, dictionary, users, role_menu } = sequelize.models;

    if (!(await dictionary.findOne())) {
        await dictionary.bulkCreate([
            {
                name: '待支付',
                type: 1
            },
            {
                name: '待收货',
                type: 1
            },
            {
                name: '已完成',
                type: 1
            },
            {
                name: '已取消',
                type: 1
            },
            {
                name: '分类',
                type: 2
            },
            {
                name: '链接',
                type: 2
            },
            {
                name: '权限',
                type: 2
            }
        ]);
    }

    if (!(await roles.findOne())) {
        await roles.create({
            name: '管理员',
            description: '可以管理所有的菜单'
        });
    }

    if (!(await users.findOne())) {
        await users.create({
            role_id: 1,
            role_name: '管理员',
            username: 'admin',
            password: md5('123123'),
            phone: '13212311231'
        });
    }

    if (!(await menu_list.findOne())) {
        await menu_list.bulkCreate([
            {
                id: 1,
                name: '角色管理',
                type: menuType.链接,
                code: 'roles',
                pid: 0,
                sort: 5
            },
            {
                id: 2,
                name: '添加角色',
                type: menuType.权限,
                code: 'rolesAdd',
                showParent: true,
                pid: 1,
                sort: 5
            },
            {
                id: 3,
                name: '修改角色',
                type: menuType.权限,
                code: 'rolesUpdate',
                pid: 1,
                sort: 10
            },
            {
                id: 4,
                name: '获取角色',
                type: menuType.权限,
                code: 'rolesAll',
                pid: 1,
                sort: 15
            },
            {
                id: 5,
                name: '用户管理',
                type: menuType.链接,
                code: 'users',
                pid: 0,
                sort: 10
            },
            {
                id: 6,
                name: '添加用户',
                type: menuType.权限,
                code: 'usersAdd',
                showParent: true,
                pid: 5,
                sort: 5
            },
            {
                id: 7,
                name: '修改用户',
                type: menuType.权限,
                code: 'usersUpdate',
                pid: 5,
                sort: 10
            },
            {
                id: 8,
                name: '删除用户',
                type: menuType.权限,
                code: 'usersDelete',
                pid: 5,
                sort: 15
            },
            {
                id: 9,
                name: '用户列表',
                type: menuType.权限,
                code: 'usersSearch',
                showParent: true,
                pid: 5,
                sort: 20
            },
            {
                id: 10,
                name: '修改密码',
                type: menuType.权限,
                code: 'usersUpdatePassword',
                pid: 5,
                sort: 25
            },
            {
                id: 11,
                name: '更改状态',
                type: menuType.权限,
                code: 'usersChangeStatus',
                pid: 5,
                sort: 30
            },
            {
                id: 12,
                name: '设置权限',
                type: menuType.权限,
                code: 'rightsSet',
                pid: 1,
                sort: 30
            },
            {
                id: 13,
                name: '用户地址列表',
                type: menuType.权限,
                code: 'usersAddressList',
                pid: 5,
                sort: 30
            },
            {
                id: 14,
                name: '添加用户地址',
                type: menuType.权限,
                code: 'usersAddressAdd',
                pid: 5,
                sort: 35
            },
            {
                id: 15,
                name: '修改用户地址',
                type: menuType.权限,
                code: 'usersAddressUpdate',
                pid: 5,
                sort: 40
            },
            {
                id: 16,
                name: '删除用户地址',
                type: menuType.权限,
                code: 'usersAddressDelete',
                pid: 5,
                sort: 45
            },
            {
                id: 17,
                name: '商品管理',
                type: menuType.分类,
                code: 'goods',
                pid: 0,
                sort: 20
            },
            {
                id: 18,
                name: '分类管理',
                type: menuType.链接,
                code: 'classify',
                pid: 17,
                sort: 5
            },
            {
                id: 19,
                name: '添加分类',
                type: menuType.权限,
                showParent: true,
                code: 'classifyAdd',
                pid: 18,
                sort: 5
            },
            {
                id: 20,
                name: '修改分类',
                type: menuType.权限,
                code: 'classifyUpdate',
                pid: 18,
                sort: 10
            },
            {
                id: 21,
                name: '删除分类',
                type: menuType.权限,
                code: 'classifyDelete',
                pid: 18,
                sort: 15
            },
            {
                id: 22,
                name: '获取分类',
                showParent: true,
                type: menuType.权限,
                code: 'classifyGet',
                pid: 18,
                sort: 20
            },
            {
                id: 23,
                name: '添加属性',
                type: menuType.权限,
                code: 'attrsAdd',
                pid: 18,
                sort: 25
            },
            {
                id: 24,
                name: '修改属性',
                type: menuType.权限,
                code: 'attrsUpdate',
                pid: 18,
                sort: 30
            },
            {
                id: 25,
                name: '删除属性',
                type: menuType.权限,
                code: 'attrsDelete',
                pid: 18,
                sort: 35
            },
            {
                id: 26,
                name: '获取属性',
                type: menuType.权限,
                code: 'attrsGet',
                pid: 18,
                sort: 39
            },
            {
                id: 27,
                name: '商品列表',
                type: menuType.链接,
                code: 'goods',
                pid: 17,
                sort: 10
            },
            {
                id: 28,
                name: '添加商品',
                showParent: true,
                type: menuType.权限,
                code: 'goodsAdd',
                pid: 27,
                sort: 5
            },
            {
                id: 29,
                name: '修改商品',
                type: menuType.权限,
                code: 'goodsUpdate',
                pid: 27,
                sort: 10
            },
            {
                id: 30,
                name: '上/下架商品',
                type: menuType.权限,
                code: 'goodsChangeStatus',
                pid: 27,
                sort: 15
            },
            {
                id: 31,
                name: '搜索商品',
                showParent: true,
                type: menuType.权限,
                code: 'goodsSearch',
                pid: 27,
                sort: 20
            },
            {
                id: 32,
                name: '图片管理',
                type: menuType.链接,
                code: 'picture',
                pid: 17,
                sort: 20
            },
            {
                id: 33,
                name: '添加图片',
                showParent: true,
                type: menuType.权限,
                code: 'pictureAdd',
                pid: 32,
                sort: 5
            },
            {
                id: 34,
                name: '删除图片',
                type: menuType.权限,
                code: 'pictureDelete',
                pid: 32,
                sort: 10
            },
            {
                id: 35,
                name: '获取图片',
                type: menuType.权限,
                code: 'pictureGet',
                pid: 32,
                sort: 15
            },
        ])
    }

    if (!(await role_menu.findOne())) {
        const menus = await menu_list.findAll({ where: { type: menuType.权限 }, attributes: ['id'] });
        await role_menu.bulkCreate(menus.map(({ id }) => ({
            role_id: 1,
            menu_id: id
        })));
    }

    console.log('同步成功');
})();